package com.gzz.orchid.common.boot.mybatis.dbraw;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.function.Function;

/**
 * @author higmd
 */
public class MyBatisRawHelper {
    private SqlSessionFactory sqlSessionFactory = null;
    public MyBatisRawHelper(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    private <R> R execute(Function<RawMapper, R> function) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            RawMapper mapper = sqlSession.getMapper(RawMapper.class);
            return function.apply(mapper);
        }
    }

    /**
     * 插入
     */
    public int insert(Object po) {
        return execute( mapper->mapper.insert(po));
    }
    /**
     * 删除
     */
    public int delete(Object po) {
        return execute( mapper->mapper.delete(po));
    }
    /**
     * 更新
     */
    public int update(Object po) {
        return execute( mapper->mapper.update(po));
    }
    /**
     * 查询
     */
    public  <E> E selectOne(Object po) {
        return (E) execute(mapper->mapper.selectOne(po));
    }
}
